home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 22 / CU Amiga Magazine's Super CD-ROM 22 (1998)(EMAP Images)(GB)[!][issue 1998-05].iso / PowerPC / Programming / PPCsiod / sources / specialforms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-22  |  8.3 KB  |  333 lines

  1. /* Scheme In One Define.
  2.  
  3. The garbage collector, the name and other parts of this program are
  4.  
  5.  *                     COPYRIGHT (c) 1989 BY                              *
  6.  *      PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS.       *
  7.  
  8. Conversion  to  full scheme standard, characters, vectors, ports, complex &
  9. rational numbers, and other major enhancments by
  10.  
  11.  *      Scaglione Ermanno, v. Pirinoli 16 IMPERIA P.M. 18100 ITALY        * 
  12.  
  13. Permission  to use, copy, modify, distribute and sell this software and its
  14. documentation  for  any purpose and without fee is hereby granted, provided
  15. that  the  above  copyright  notice appear in all copies and that both that
  16. copyright   notice   and   this  permission  notice  appear  in  supporting
  17. documentation,  and that the name of Paradigm Associates Inc not be used in
  18. advertising or publicity pertaining to distribution of the software without
  19. specific, written prior permission.
  20.  
  21. PARADIGM  DISCLAIMS  ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  22. ALL  IMPLIED  WARRANTIES  OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  23. PARADIGM  BE  LIABLE  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  24. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  25. IN  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  26. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  
  28. */
  29.  
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33. #include <setjmp.h>
  34. #include <signal.h>
  35. #include <math.h>
  36. #include <limits.h>
  37.  
  38. #include "siod.h"
  39.  
  40.  
  41.  
  42. LISP leval_if(LISP *pform,LISP *penv)
  43. {LISP args,env;
  44.  args = cdr(*pform);
  45.  env = *penv;
  46.  if NNULLP(leval(car(args),env)) 
  47.     *pform = car(cdr(args)); 
  48.  else 
  49.     *pform = car(cdr(cdr(args)));
  50.  return(truth);}
  51.  
  52. LISP leval_while(LISP *pform,LISP *penv)
  53. {LISP test,body,env,ans;
  54.  ans = NIL;
  55.  test = car(cdr(*pform));
  56.  body = cdr(cdr(*pform));
  57.  env = *penv;
  58.  while(NNULLP(leval(test,env)))
  59.     ans = leval(cons(sym_progn,body),env);
  60.  *pform = ans;
  61.  return(NIL);}
  62.  
  63. LISP env_prep(LISP env)
  64. {LISP l,tmp;
  65.  LISP res = NIL;
  66.  for(l=env;CONSP(l);l=CDR(l))
  67.    {tmp = CAR(l);
  68.     res = cons(cons(car(tmp),cons(car(cdr(cdr(tmp))),cons(NIL,NIL))),res);}
  69.  return(res);}
  70.  
  71. LISP leval_do(LISP *pform,LISP *penv)
  72. {LISP vars,test,exit,body,env,ans;
  73.  ans = NIL;
  74.  vars = car(cdr(*pform));
  75.  test = car(cdr(cdr(*pform)));
  76.  exit = cdr(test);
  77.  test = car(test);
  78.  body = cdr(cdr(cdr(*pform)));
  79.  if(NULLP(cdr(body)))
  80.     body = car(body);
  81.  else
  82.     body = cons(sym_progn,body);
  83.  env = *penv;
  84.  env_test(vars);
  85.  env = envcons(leval_let_env(vars,env),env);
  86.  *penv = env;
  87.  vars = env_prep(vars);
  88.  while(NULLP(ans = leval(test,env)))
  89.    {leval(body,env);
  90.    do_increment(vars,env);}
  91.  if(NULLP(exit))
  92.    {*pform = ans;
  93.     return NIL;}
  94.  else 
  95.    {if(NULLP(cdr(exit)))
  96.       *pform = car(exit);
  97.     else 
  98.       *pform = cons(sym_progn,exit);}
  99.  return(truth);}
  100.  
  101. void do_increment(LISP vars,LISP env)
  102. {LISP l,tmp,inc;
  103.  for(l=vars;CONSP(l);l=CDR(l))
  104.    {tmp = CAR(l);
  105.     inc = CAR(CDR(tmp));
  106.     if(NNULLP(inc))
  107.        CAR(CDR(CDR(tmp))) = leval(inc,env);}
  108.  for(l=vars;CONSP(l);l=CDR(l))
  109.    {tmp = CAR(l);
  110.     inc = CAR(CDR(tmp));
  111.     if(NNULLP(inc))
  112.        setvar(CAR(tmp),CAR(CDR(CDR(tmp))),env);}}
  113.  
  114. LISP leval_foreach(LISP args,LISP env)
  115. {LISP tmp,ptr;
  116.  tmp = leval(car(cdr(args)),env);
  117.  ptr = leval(car(args),env);
  118.  if (NULLP(tmp))
  119.    return(NIL);
  120.   if(!procp(ptr))
  121.    err("for-each",ptr,ERR_FIRST | ERR_NPRO);
  122.  if (NCONSP(tmp))
  123.    err("for-each",tmp,ERR_SECOND | ERR_NPAI);
  124.  while(CONSP(tmp))
  125.   {apply_proc(ptr,cons(CAR(tmp),NIL),env);
  126.    tmp = CDR(tmp);}
  127.  if (NNULLP(tmp))
  128.    err("improper list to for-each",tmp,ERR_GEN);
  129.  return(NIL);}
  130.  
  131. LISP leval_map(LISP args,LISP env)
  132. {LISP tmp,ptr;
  133.  LISP y,*z;
  134.  tmp = leval(car(cdr(args)),env);
  135.  ptr = leval(car(args),env);
  136.  if (NULLP(tmp))
  137.    return(NIL);
  138.  if(!procp(ptr))
  139.    err("map",ptr,ERR_FIRST | ERR_NPRO);
  140.  if (NCONSP(tmp))
  141.    err("map",tmp,ERR_SECOND | ERR_NPAI);
  142.  y = NIL;
  143.  z = &y;
  144.  while(CONSP(tmp))
  145.   {*z = cons(apply_proc(ptr,cons(CAR(tmp),NIL),env),NIL);
  146.    tmp = CDR(tmp);
  147.    z = &CDR(*z);}
  148.  if (NNULLP(tmp))
  149.    err("improper list to map",tmp,ERR_GEN);
  150.  return(y);}
  151.  
  152. LISP leval_when(LISP *pform,LISP *penv)
  153. {LISP args,env;
  154.  args = cdr(*pform);
  155.  env = *penv;
  156.  if NNULLP(leval(car(args),env)) 
  157.    {if(NULLP(cdr(cdr(args))))
  158.        args = car(cdr(args));
  159.     else
  160.        args = cons(sym_progn,cdr(args));
  161.     *pform = args;
  162.     return(truth);}
  163.  *pform = NIL; 
  164.  return(NIL);}
  165.  
  166. LISP leval_case(LISP *pform,LISP *penv)
  167. {LISP args,env,exp,test,tmp;
  168.  args = cdr(*pform);
  169.  env = *penv;
  170.  exp = leval(car(args),env);
  171.  args = cdr(args);
  172.  while CONSP(args)
  173.   {tmp = car(CAR(args));
  174.    if(NCONSP(tmp))
  175.     test = EQ(tmp,sym_else) ? truth : eql(exp,tmp);
  176.    else
  177.     test = memv(exp,tmp);
  178.    if(NNULLP(test))
  179.     {tmp=cdr(CAR(args));
  180.      if(NULLP(cdr(tmp)))
  181.        args = car(tmp);
  182.     else
  183.        args = cons(sym_progn,tmp);
  184.     *pform = args; 
  185.     return(truth);}
  186.    args = CDR(args);}
  187.  *pform = NIL; 
  188.  return(NIL);}
  189.  
  190. LISP leval_cond(LISP *pform,LISP *penv)
  191. {LISP args,env,tmp;
  192.  args = cdr(*pform);
  193.  env = *penv;
  194.  while CONSP(args)
  195.   {tmp = CAR(args);
  196.    if(EQ(car(tmp),sym_else) || NNULLP(leval(car(tmp),env)))
  197.     {if(NULLP(cdr(cdr(tmp))))
  198.        args = car(cdr(tmp));
  199.     else
  200.        args = cons(sym_progn,cdr(tmp));
  201.     *pform = args; 
  202.     return(truth);}
  203.    args = CDR(args);}
  204.  *pform = NIL; 
  205.  return(NIL);}
  206.                          
  207. LISP leval_progn(LISP *pform,LISP *penv)
  208. {LISP env,l,next;
  209.  env = *penv;
  210.  l = cdr(*pform);
  211.  next = cdr(l);
  212.  while(CONSP(next)) {leval(car(l),env);l=next;next=CDR(next);}
  213.  *pform = car(l); 
  214.  return(truth);}
  215.  
  216. LISP leval_progn0(LISP *pform,LISP *penv)
  217. {LISP env,l,res;
  218.  env = *penv;
  219.  l = cdr(*pform);
  220.  res = leval(car(l),env); 
  221.  l = cdr(l);
  222.  while(CONSP(l)) {leval(CAR(l),env);l=CDR(l);}
  223.  *pform = res; 
  224.  return(truth);}
  225.  
  226. LISP leval_or(LISP *pform,LISP *penv)
  227. {LISP env,l,next,val;
  228.  env = *penv;
  229.  l = cdr(*pform);
  230.  next = cdr(l);
  231.  while(CONSP(next))
  232.    {val = leval(car(l),env);
  233.     if NNULLP(val) {*pform = val; return(NIL);}
  234.     l=next;next=CDR(next);}
  235.  *pform = car(l); 
  236.  return(truth);}
  237.  
  238. LISP leval_and(LISP *pform,LISP *penv)
  239. {LISP env,l,next;
  240.  env = *penv;
  241.  l = cdr(*pform);
  242.  if NULLP(l) {*pform = truth; return(NIL);}
  243.  next = cdr(l);
  244.  while(CONSP(next))
  245.    {if NULLP(leval(car(l),env)) {*pform = NIL; return(NIL);}
  246.     l=next;next=CDR(next);}
  247.  *pform = car(l); 
  248.  return(truth);}
  249.  
  250. LISP leval_catch(LISP args,LISP env)
  251. {struct catch_frame frame;
  252.  int k;
  253.  LISP l,val;
  254.  val = NIL;
  255.  frame.tag = leval(car(args),env);
  256.  frame.next = catch_framep;
  257.  k = setjmp(frame.cframe);
  258.  catch_framep = &frame;
  259.  if (k == 2)
  260.    {catch_framep = frame.next;
  261.     return(frame.retval);}
  262.  for(l=cdr(args); CONSP(l); l = CDR(l))
  263.    val = leval(CAR(l),env);
  264.  catch_framep = frame.next;
  265.  return(val);}
  266.  
  267. LISP lthrow(LISP tag,LISP value)
  268. {struct catch_frame *l;
  269.  for(l=catch_framep; l; l = (*l).next)
  270.    if EQ((*l).tag,tag)
  271.      {(*l).retval = value;
  272.       longjmp((*l).cframe,2);}
  273.  err("no *catch found with this tag",tag,ERR_GEN);
  274.  return(NIL);}
  275.  
  276. LISP leval_quote(LISP args,LISP env)
  277. {return(car(args));}
  278.  
  279. LISP quasiquote_rec(LISP arg,LISP env)
  280. {LISP res ,*z;
  281.  LISP tmp,tmp2;
  282.  if NCONSP(arg) return(arg);
  283.  res = NIL;
  284.  z = &res;
  285.  while(CONSP(arg))
  286.   {tmp=CAR(arg);
  287.    if(NCONSP(tmp))
  288.     {*z = cons(tmp,NIL);
  289.      z = &CDR(*z);}
  290.    else if(EQ(cintern("quasiquote"),car(tmp)))
  291.     {*z = cons(tmp,NIL);
  292.      z = &CDR(*z);}
  293.    else if(EQ(cintern("unquote"),car(tmp)))
  294.     {*z = cons(leval(car(cdr(tmp)),env),NIL);
  295.      z = &CDR(*z);}
  296.    else if(EQ(cintern("unquote-splicing"),car(tmp)))
  297.     {*z = copy_list(leval(car(cdr(tmp)),env));
  298.      if(NNULLP(*z))
  299.        {if(NCONSP(*z))
  300.           err("unquote-splicing",*z,ERR_GEN_ARG | ERR_NPAI);
  301.         for(tmp2=*z;CONSP(cdr(tmp2));tmp2=cdr(tmp2));
  302.         z=&tmp2;
  303.         z = &CDR(*z);}}
  304.    else if(EQ(cintern("+internal-comma-dot"),car(tmp)))
  305.     {*z = leval(car(cdr(tmp)),env);
  306.      if(NNULLP(*z))
  307.        {if(NCONSP(*z))
  308.           err("unquote-splicing",*z,ERR_GEN_ARG | ERR_NPAI);
  309.         for(tmp2=*z;CONSP(cdr(tmp2));tmp2=cdr(tmp2));
  310.         z=&tmp2;
  311.         z = &CDR(*z);}}
  312.    else
  313.     {*z = cons(quasiquote_rec(tmp,env),NIL);
  314.      z = &CDR(*z);}
  315.    arg = cdr(arg);}
  316.   *z = arg;
  317.   return(res);}
  318.  
  319. LISP leval_quasiquote(LISP *pargs,LISP *penv)
  320. {LISP env,list;
  321.  env = *penv;
  322.  list = car(cdr(*pargs));
  323.  *pargs = cons(sym_quote,cons(quasiquote_rec(list,env),NIL));
  324.  return(truth);}
  325.  
  326. LISP quit(void)
  327. {longjmp(errjmp,2);
  328.  return(NIL);}
  329.  
  330. LISP reset(void)
  331. {longjmp(errjmp,1);
  332.  return(NIL);}
  333.